package md.frolov.bible.editors;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

import md.frolov.bible.Constants;
import md.frolov.bible.model.Archive;
import md.frolov.bible.model.Book;
import md.frolov.bible.model.Chapter;
import md.frolov.bible.model.IStructure;
import md.frolov.bible.model.Module;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;

public class BibleEditorInput implements IEditorInput {
	private final Chapter chapter;
	private final int[] selectedIxs;
	
	public BibleEditorInput(String url){
		Matcher m = Constants.urlPattern.matcher(url);
		
		
		if(!m.matches()) throw new IllegalArgumentException();
		
		chapter = getChapter(m.group(1), m.group(2), m.group(3));
		selectedIxs = getSelectedIxs(m.group(4));
	}
	
	private int[] getSelectedIxs(String ids){
		if(ids==null) return new int[]{};
		if(ids.equals("")) return new int[]{};
		
		try{
			List<Integer> ixs = new ArrayList<Integer>();
			int a;
			int b;
			int t;
			
			String[] strs = ids.split(",");
			for(int i=0;i<strs.length;i++){
				t = strs[i].indexOf('-');
				if(t>=0){
					a = Integer.valueOf(strs[i].substring(0,t));
					b = Integer.valueOf(strs[i].substring(t+1));
					if(b<=a) throw new IllegalArgumentException();
					for(int j=a; j<=b; j++)
						ixs.add(j);
				} else {
					ixs.add(Integer.valueOf(strs[i]));
				}
			}
			
			int[] result = new int[ixs.size()];
			for(int i=0;i<ixs.size();i++)
				result[i] = ixs.get(i);
			
			//sort
			for(int i=0;i<result.length;i++)
				for(int j=i;j<result.length;j++)
					if(result[j]<result[i]){
						a = result[j];
						result[j] = result[i];
						result[i] = a;
					}
			
			return result;
		} catch(NumberFormatException e){
			throw new IllegalArgumentException();
		}
	}
	
	private Chapter getChapter(String moduleId, String bookId, String chapterId){
		Module module;
		Book book;
		Chapter chapter;
		
		module = Archive.getInstance().getModuleByID(moduleId);
		
		if(bookId == null) {
			book = module.getFirstBook();
			chapter = book.getFirstChapter();
			return chapter;
		}
		book = module.getBookByLocalId(bookId);
		
		if(chapterId == null){
			chapter = book.getFirstChapter();
			return chapter;
		}
		chapter = book.getChapterByLocalId(chapterId);
		
		if(chapter == null) throw new IllegalArgumentException();
		return chapter;
	}
	
	
	public BibleEditorInput(IStructure str){
		chapter = getChapter(str);
		selectedIxs = new int[]{};
	}
	
	private Chapter getChapter(IStructure str){
		Chapter chapter = null;
		if(str==null) throw new IllegalArgumentException();
		if(str instanceof Chapter){
			chapter = (Chapter) str;
			return chapter;
		}
		Object[] ch = str.getChildren();
		if(ch[0] instanceof IStructure) chapter = getChapter((IStructure)ch[0]);
		if(chapter == null) throw new IllegalArgumentException();
		return chapter;
	}
	
	@Override
	public boolean exists() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		return chapter.getBook().getName()+": "+chapter.getName();
	}
	
	public String getYok(){
		StringBuilder sb = new StringBuilder();
		sb.append(chapter.getBook().getName()).append(' ').append(chapter.getLocalId());
		if(selectedIxs.length>0){
			sb.append(':');
			for(int i = 0; i<selectedIxs.length;i++){
				if(i>=1 && i<=selectedIxs.length-2)
					if(selectedIxs[i+1]-selectedIxs[i-1]==2) continue;
				sb.append(selectedIxs[i]);
				if(i==selectedIxs.length-1) continue;
				if(selectedIxs[i+1]-selectedIxs[i]>1){
					sb.append(',');
					continue;
				}
				if(selectedIxs[i+1]-selectedIxs[i]==1){
					sb.append('-');
					continue;
				}
			}
		}
		return sb.toString();
	}

	@Override
	public IPersistableElement getPersistable() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getToolTipText() {
		return "ToolTip"; //TODO
	}

	@Override
	public Object getAdapter(Class adapter) {
		if (adapter.isInstance(Chapter.class)) return chapter;
		return null;
	}

	public Chapter getChapter(){
		return chapter;
	}
	
	public int[] getSelectedIndexes(){
		return selectedIxs;
	}
}
